package mdv.exp.items.chooser;

import android.content.Context;
import android.database.Cursor;
import android.database.DataSetObserver;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ListAdapter;

import com.example.data.MyDataBaseHelper;
import com.example.expenses.ItemsActivity;
import com.example.expenses.R;
import com.example.expenses.R.id;

public class ItemsChooserAdapter extends ArrayAdapter{
	
	private static final String TAG = "MultichoiceAdapter";
	private MyDataBaseHelper db;
	private Cursor cursor;
	private Context context;
	private String columnName;

	public ItemsChooserAdapter(Context context, int multiChoiceItem,
			int supplierHeaderTag) {
		  super(context, R.id.multi_choice_item);
		  
		  switch (supplierHeaderTag) {
			case ItemsActivity.SUPPLIER_HEADER_TAG:
				this.columnName = MyDataBaseHelper._SUPPLIER_NAME;
				break;
			case ItemsActivity.KIND_HEADER_TAG:
				this.columnName = MyDataBaseHelper._KIND;
				break;

			default:
				break;
			}
		  
		  this.context = context;
		  db = new MyDataBaseHelper(context);
	      db.openDataBase();
	      cursor = db.getDistinct(columnName);
	      Log.d(TAG,"MultichoiceAdapter " + cursor.getCount());
	      cursor.moveToFirst();
	      this.context = context;
	      
		
	}

	@Override
	public int getCount() {
		//Log.d(TAG,"getCount " + cursor.getCount());
		return  cursor.getCount();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		//Log.d(TAG,"getView: " + position);
		ItemsChooserRowView view;
		if(convertView == null){
			view = new ItemsChooserRowView(context);	
		}else
			view = (ItemsChooserRowView) convertView;
		
		cursor.moveToPosition(position);
		
		view.setName(cursor.getString(cursor.getColumnIndex(this.columnName)));
		
		return view;
	}

	public void closeDB() {
		db.close();
		
	}

}
